﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using AutoMapper;
using EFEntity;
using Iservices;
using Microsoft.EntityFrameworkCore;
using Model;

namespace Services
{
    public class config_file_second_kindDao : Iconfig_file_second_kindDao
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;

        public config_file_second_kindDao(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }

        public async Task<int> Add(config_file_second_kind c)
        {
            int result = 0;
            EFconfig_file_second_kind etest = mapper.Map<EFconfig_file_second_kind>(c);
            
            myDbContext.second_kind.Add(etest);


            result = await myDbContext.SaveChangesAsync();

            return result;
        }

        public async  Task<int> Del(int id)
        {
            int result = 0;


            EFconfig_file_second_kind s = myDbContext.second_kind.Find(id);
            myDbContext.second_kind.Remove(s);
            result = await myDbContext.SaveChangesAsync();


            return result;
        }

        public async Task<List<config_file_second_kind>> firstSelect()
        {
            List<EFconfig_file_second_kind> list = new List<EFconfig_file_second_kind>();
            await Task.Run(() => {
                list = myDbContext.second_kind.ToList();
            });

            //EFconfig_file_first_kind list2 = mapper.Map<EFconfig_file_first_kind>(list);
            List<config_file_second_kind> list2 = mapper.Map<List<config_file_second_kind>>(list);
            //转换动作:EO-DTO
          
            return list2;
        }

       

        public async Task<int> Update(config_file_second_kind c)
        {
            EFconfig_file_second_kind etest = mapper.Map<EFconfig_file_second_kind>(c);
            myDbContext.Attach(etest);
            myDbContext.Entry<EFconfig_file_second_kind>(etest).State = EntityState.Modified;
            return await myDbContext.SaveChangesAsync();
        }
    }
}
